package com.parkinglot.management.controller;

import com.parkinglot.management.repository.entity.EnteredCarInfo;
import com.parkinglot.management.service.EnteredCarInfoService;
import com.parkinglot.management.util.SaveImg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@Api("出入停车场车辆信息记录")
@RequestMapping("/carInfo")
public class EnteredCarInfoController {

    @Autowired
    SaveImg saveImg;

    @Autowired
    EnteredCarInfoService enteredCarInfoService;

    private  final SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
    @ApiOperation("添加进入停车场车辆信息")
    @PostMapping
    public EnteredCarInfo addEnteredCarInfo(@RequestBody MultipartFile file){
        String enteredTime = df.format(new Date());
        String spliceImgPath = saveImg.saveUploadedImg(file,enteredTime);
        return enteredCarInfoService.addEnteredCarInfo(spliceImgPath.toString(), enteredTime);
    }

    @ApiOperation("删除进入停车场车辆信息")
    @DeleteMapping
    public void deleteEnteredCarInfo(EnteredCarInfo enteredCarInfo){
        enteredCarInfoService.deleteEnteredCarInfo(enteredCarInfo);
    }
    @ApiOperation("得到停车场内的所有车辆信息")
    @GetMapping
    public List<EnteredCarInfo> getAllEnteredCarInfo(){
        return enteredCarInfoService.getAllEnteredCarInfo();
    }

    @ApiOperation("根据车牌号查找停车场内的车辆信息")
    @GetMapping("/{carNum}")
    public List<EnteredCarInfo> getEnteredCarInfoByCarNum(@PathVariable String carNum){
        return enteredCarInfoService.getEnteredCarInfoByCarNum(carNum);
    }

}
